#include "md5.h"
int get_md5(const char * filename, char * md5_val, off_t  maxlen)
{

    MD5_CTX ctx;
    MD5_Init(&ctx);
    int fd = open(filename, O_RDONLY);
    char buf[4096];
    off_t cur = 0;
    off_t min = -1;
    while(cur < maxlen){
        min = sizeof(buf) < maxlen - cur ? sizeof(buf) : maxlen - cur;
        bzero(buf,sizeof(buf));
        ssize_t sret = read(fd,buf, min);
        if(sret == 0){
            break;
        }
        MD5_Update(&ctx,buf,sret);
        cur += sret;
    }
    unsigned char md[16]; // 申请16字节的空间
    MD5_Final(md,&ctx);
    char num[3] = {0};
    for(int i = 0; i < 16; ++i){
        sprintf(num,"%02x",md[i]); // 1字节二进制 -->2个 16进制
        strcat(md5_val, num);
    }

    close(fd);
    return 0;
}

